package clients;

import java.awt.BorderLayout;

import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Test04Client extends JFrame implements ActionListener{
	JTextArea msg = new JTextArea();
    JTextArea userInfo = new JTextArea(1, 10);
    JTextField jf = new JTextField();
    JButton jb = new JButton("发送");
    
    Socket clientSocket;
    OutputStream os;
    PrintWriter pw;
    
	public static void main(String [] args){
		Test03Client mc = new Test03Client();
	}
	
	public Test04Client() {
		super("四方精创11楼聊天群");
		setLayout(new BorderLayout(5,5));
		JPanel p = new JPanel(new BorderLayout(5,5));
		JPanel jp = new JPanel(new BorderLayout(5,5));
        jb.addActionListener(this);
        msg.setEditable(false);
        userInfo.setEditable(false);
       
        //定义带滚动条的panel，并将JTextArea存入到panel中，使textarea具有滚动条显示功能。
        JScrollPane scrollMsg = new JScrollPane(msg);
        JScrollPane scrollUser = new JScrollPane(userInfo);
        //取消显示水平滚动条
        scrollMsg.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollUser.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        //显示垂直滚动条
        scrollMsg.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollUser.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        
        jp.add("Center", jf);
        jp.add("East", jb);
        p.add("Center", scrollMsg);
        p.add("South", jp);
        add("Center", p);
        add("East", scrollUser);
        
        super.setBounds(450, 200, 400, 300);
        super.setVisible(true);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        connect();
        super.addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//关闭资源
		        try {
		        	if(pw != null) {
		        		pw.close();
		        	}
		        	if(os != null) {
		        		os.close();
		        	}
		        	if(clientSocket != null) {
		        		clientSocket.close();
		        	}
				} catch (IOException ioex) {
					// TODO Auto-generated catch block
					ioex.printStackTrace();
				}
			}

			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
        	
        });
	}

	//建立连接
	private void connect() {
		//发送到 ip 9999 端口
		try {
			clientSocket = new Socket("127.0.0.1", 9999);
			//输出流
	        os = clientSocket.getOutputStream();
	        pw = new PrintWriter(new OutputStreamWriter(os));       
	        pw.println("Anan：" + "连接成功");
	        pw.flush();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == jb) {
			pw.println("Anan：" + jf.getText());
	        pw.flush();
			jf.setText("");
		}
	}
}

